package homework13;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;


public class mycallable implements Callable<StringBuilder> {

    ArrayList<Integer> list;
    Integer sumprize = 0;
    Integer maxprize=0;

    public mycallable(ArrayList<Integer> list) {
        this.list = list;
    }

    public Integer getmaxprize(){
        for (int i = 0; i < list.size(); i++) {
            int p= list.get(i);
            if (p>maxprize){
                maxprize=p;
            }
        }
        return maxprize;
    }
    public Integer getSumprize() {
        return sumprize;
    }

    public void setSumprize(Integer sumprize) {
        this.sumprize = sumprize;
    }

    @Override
    public StringBuilder call() throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append(Thread.currentThread().getName() + ":");
        Integer max=getmaxprize();
        while (true) {
            synchronized (home01.class) {
                if (list.size() == 0) {
                    break;
                }
                Collections.shuffle(list);
                Integer prize = list.remove(0);
                setSumprize(sumprize + prize);
//                System.out.print(prize+",");
                sb.append(prize + ",");
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        sb.append("总计额为"+getSumprize()+"元");
        if(sb.indexOf(String.valueOf(maxprize))==-1/*sb.toString().split(",").toString().contains(max.toString())*/){
            return sb;
        }else{
            sb.append("最大金额为"+Thread.currentThread().getName()+"中的"+max+"元大奖");
            return sb;
        }
    }
}
